import nyNetwork from "@/api/nyBaseNetwork";
import DZModelPage from "@/model/DZModelPage";
import dzMsgBox from "@/utils/DZMessageBox";
import uploadFile from "@/utils/DZUploadFile";
import DZModelVideo from "../model/DZModelVideo";
import DZModelType from "@/views/视频管理/model/DZModelVideoType";

class DZServiceVideo {

    list_video: Array<DZModelVideo> = [];

    m_video = new DZModelVideo();

    m_page = new DZModelPage(10);

    list_type: Array<DZModelType> = [];

    m_type = new DZModelType();

    net_add_type(success: () => void) {
        nyNetwork.net("添加视频分类", { ...this.m_type }, data => {
            this.m_type = new DZModelType();
            success();
        })
    }

    net_edit_type(success: () => void) {
        nyNetwork.net("修改视频分类", { ...this.m_type }, data => {
            this.m_type = new DZModelType();
            success();
        })
    }

    net_del_type(success: () => void) {
        nyNetwork.net("删除视频分类", { id: this.m_type.id }, data => {
            success();
        })
    }

    net_list_type(cur_page) {
        var arr_data = [];
        nyNetwork.net("视频分类列表", { page:cur_page }, data => {
            this.m_page = new DZModelPage(10);
            this.m_page.page = cur_page;
            this.m_page.total = data.total;
            for (const item of data.list) {
                var type = new DZModelType();
                type.id = item.iId ?? '';
                type.logo = item.iLogoId + '' ?? '';
                if (item.iLogoId) {
                    type.logo_url = nyNetwork.image(item.iLogoId)
                }
                type.name = item.sName ?? '';
                type.sort = item.iSort ?? 0;
                arr_data.push(type);
            }
            this.list_type = arr_data;
        })
    }

    net_list_type_dic() {
        var arr_data = [];
        nyNetwork.net("视频分类字典列表", {}, data => {
            for (const item of data) {
                var type = new DZModelType();
                type.id = item.iId ?? '';
                type.name = item.sName ?? '';
                arr_data.push(type);
            }
            this.list_type = arr_data;
        })
    }

    net_add_video(success: () => void) {
        nyNetwork.net("添加视频", { ...this.m_video }, data => {
            success();
        })
    }

    net_edit_video(success: () => void) {
        nyNetwork.net("修改视频", { ...this.m_video }, data => {
            success();
        })
    }

    net_list_video() {
        var arr_data = [];
        nyNetwork.net("视频列表", { title: this.m_video.title, ...this.m_page ,typeId: this.m_video.typeId,}, data => {
            this.m_page.total = data.total;
            for (const item of data.list) {
                var video = new DZModelVideo();
                video.id = item.iId ?? '';
                video.cover = item.iCoverId ?? '';
                if (item.iCoverId) {
                    video.coverUrl = nyNetwork.image(item.iCoverId);
                }
                video.title = item.sTitle ?? '';
                video.desc = item.sDesc ?? '';
                video.video = item.iVideoId ?? '';
                video.lookNum = item.iLookNum ?? 0;
                video.time = item.createTime ?? '';
                video.time = video.time.substring(0, video.time.indexOf(" "));
                video.typeName = item.sTypeName ?? '';
                video.typeId = item.iTypeId ?? 0;
                video.sort = item.iSort ?? 0;
                video.top = item.iTop ?? 0;
                arr_data.push(video);
            }
            this.list_video = arr_data;
        })
    }

    net_list_video_all() {
        var arr_data = [];
        nyNetwork.net("所有视频", {}, data => {
            for (const item of data) {
                var video = new DZModelVideo();
                video.id = item.iId ?? '';
                video.title = item.sTitle ?? '';
                arr_data.push(video);
            }
            this.list_video = arr_data;
        })
    }

    net_video_info() {
        nyNetwork.net("视频详情", { id: this.m_video.id }, data => {
            this.m_video.prase(data);
        })
    }

    net_upload_image(file) {
        uploadFile.uploadImage(file, () => {
        }, (res) => {
            this.m_video.cover = res;
        }, () => {
            this.m_video.coverUrl = '';
        })

    }

    net_upload_video(file) {
        uploadFile.uploadVideo(file, (res) => {
            this.m_video.video = res;
        }, () => {
            this.m_video.videoUrl = '';
        })

    }

    net_del_video(id, success: () => void) {
        dzMsgBox.del("是否删除该视频", () => {
            nyNetwork.net("删除视频", { id: id }, data => {
                success();
            })
        })
    }

    net_edit_sort(id,sort,success: () => void) {
        nyNetwork.net("修改视频排序", { id: id,sort:sort }, data => {
            success();
        })
    }

    
    net_move_up(id, success: () => void) {
        nyNetwork.net("视频数据上移", { id: id }, data => {
            success();
        })
    }
    
    net_move_down(id, success: () => void) {
        nyNetwork.net("视频数据下移", { id: id }, data => {
            success();
        })
    }

    net_move_top(id, success: () => void) {
        nyNetwork.net("视频数据置顶", { id: id }, data => {
            success();
        })
    }

    net_cancel_top(id, success: () => void) {
        nyNetwork.net("视频取消置顶", { id: id }, data => {
            success();
        })
    }
}
export default DZServiceVideo;